<template>
    <div class="login_mbl">
        <h1 class="title">后台管理系统</h1>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="账号" prop="username">
                <el-col :span="6">
                    <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
                </el-col>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-col :span="6">
                    <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                </el-col>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import Cookies from 'js-cookie'
    import { login } from '@/api/login'
    export default {
        name: "index",
        data() {
            return {
                ruleForm: {
                    username: '',
                    password: '',
                },
                rules: {
                    username: [
                        { required: true, message: '请填写用户名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请填写密码', trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate(async (valid) => {
                    if (valid) {
                        const res = await login({
                          username: this.ruleForm.username,
                          password: this.ruleForm.password
                        })
                      Cookies.set('token', res.token)
                      // 跳转页面
                      this.$router.push('/SystemManage')
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style lang="scss" scoped>
    .login_mbl{
        .title{
            text-align: center;
            margin-bottom: 40px;
            letter-spacing: 5px;
        }
       /* background: #7e8c8d;*/
        margin-top: 180px;
        /deep/ .el-form-item {
            margin-bottom: 32px;
            margin-left: 520px;
        }
        /deep/ .el-button--medium {
            font-size: 14px;
        }

    }

</style>
